package com.tencent.crazypenguin.dao;

import org.hibernate.Session;

import com.tencent.crazypenguin.config.HbmUtil;

public class BaseDao<T> {
	

	@SuppressWarnings("unchecked")
	public T findById(Long id,T c) {
		
		Session s = HbmUtil.getSession();

		try{
			
			T t = (T) s.get(c.getClass(), id);
	
			return t;

		}finally{

			if(s!=null){
	
				s.close();
	
			}

		}

	}

		//删除用户

	public void remove(T entity) {
		
		Session s = HbmUtil.getSession();

		try{

			s.beginTransaction();
	
			s.delete(entity);
	
			s.getTransaction().commit();

		}finally{

			if(s!=null){
	
				s.close();
	
			}

		}

	}

		//添加一个用户

	public void save(T entity) {
		
		Session s = HbmUtil.getSession();

		try{

			s.beginTransaction();
	
			s.save(entity);
	
			s.getTransaction().commit();

		}finally{

			if(s!=null){
	
				s.close();
	
			}

		}

	}

		//更新一个用户

	public void update(T entity) {

		Session s = HbmUtil.getSession();

		try{

			s.beginTransaction();
	
			s.update(entity);
	
			s.getTransaction().commit();

		}finally{

			if(s!=null){
	
			s.close();
	
			}

		}
	}

}
